package com.example.demo.common.utils.demo;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.utils.BaseResponse;
import com.example.demo.common.utils.HttpUtil;
import com.example.demo.common.utils.RSAHelper;
import com.example.demo.common.utils.demo.common.Config;
import com.example.demo.common.utils.demo.model.OpenApiRechargeRequest;
import com.example.demo.common.utils.demo.model.OpenApiWithDrawRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhangws email:1127145966@qq.com
 * @Description: 余额查询接口调用demo
 * @Date:2019/7/22 0022 10:05
 */
public class Balance {
    static Logger log = LoggerFactory.getLogger(Balance.class);
    /**
     * 余额查询
     */
    public static void balanceQuery(String mainstayId,String payType){

        String url = Config.baseUrl+"/open/api/v1/balance/query";
        String paramsJson = "{\"mainstayId\":\"" + mainstayId + "\",\"payType\":\"" + payType + "\"}";
        BaseResponse baseResponse = HttpUtil.post(url,paramsJson,Config.appKey,Config.secretkey,Config.publicKey);
        if (baseResponse.isSuccess()){
            String rs = baseResponse.getData().toString();
            //公钥解密
            if (StringUtils.isNotBlank(rs)){
                try {
                    String dec = RSAHelper.decryptByPublicKey(rs,Config.publicKey);
                    log.info(String.format("解密结果:%s",dec));
                }catch (Exception e){
                    log.info("publicKey 有误");
                }
            }
        }
    }
    /**
     * 充值
     */
    public static void recharge(OpenApiRechargeRequest openApiRechargeRequest){
        String url = Config.baseUrl+"/open/api/v1/balance/recharge";
        String paramsJson = JSONObject.toJSONString(openApiRechargeRequest);
        BaseResponse baseResponse = HttpUtil.post(url,paramsJson,Config.appKey,Config.secretkey,Config.publicKey);
        if (baseResponse.isSuccess()){
            String rs = baseResponse.getData().toString();
            //公钥解密
            if (StringUtils.isNotBlank(rs)){
                try {
                    String dec = RSAHelper.decryptByPublicKey(rs,Config.publicKey);
                    log.info(String.format("解密结果:%s",dec));
                }catch (Exception e){
                    log.info("publicKey 有误");
                }
            }
        }
    }

    /**
     * 提现
     */
    public static void withdraw(OpenApiWithDrawRequest openApiWithDrawRequest){
        String url = Config.baseUrl+"/open/api/v1/balance/withdraw";
        String paramsJson = JSONObject.toJSONString(openApiWithDrawRequest);
        BaseResponse baseResponse = HttpUtil.post(url,paramsJson,Config.appKey,Config.secretkey,Config.publicKey);
        if (baseResponse.isSuccess()){
            String rs = baseResponse.getData().toString();
            //公钥解密
            if (StringUtils.isNotBlank(rs)){
                try {
                    String dec = RSAHelper.decryptByPublicKey(rs,Config.publicKey);
                    log.info(String.format("解密结果:%s",dec));
                }catch (Exception e){
                    log.info("publicKey 有误");
                }
            }
        }
    }

}
